package com.crayfish.materiallearn;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.graphics.Palette;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 作者：crayfish(徐杰)
 * 描述：
 * Created by crayfish on 2017/5/3.
 */

public class AnimatorActivity extends AppCompatActivity {

    private Button btn_reveal;
    private TextView txt_reveal;
    private FloatingActionButton floatingActionButton;
    private ImageView imageView;
    private View view;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animator);
        initView();
        initEvent();
    }
    private void initView(){
        btn_reveal = (Button)super.findViewById(R.id.btn_reveal);
        txt_reveal = (TextView)super.findViewById(R.id.txt_reveal);
        floatingActionButton = (FloatingActionButton)super.findViewById(R.id.floatingButton);
        imageView = (ImageView)super.findViewById(R.id.image);
        view = super.findViewById(R.id.view);
    }
    private void initEvent(){
        btn_reveal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(txt_reveal.getVisibility() == View.VISIBLE) {
                    int cx = (txt_reveal.getLeft() + txt_reveal.getRight()) / 2;
                    int cy = (txt_reveal.getTop() + txt_reveal.getBottom()) / 2;
                    int initialRadius = txt_reveal.getWidth();
                    Animator anim = ViewAnimationUtils.createCircularReveal(txt_reveal, cx, cy, initialRadius, 0);
                    anim.addListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            super.onAnimationEnd(animation);
                            txt_reveal.setVisibility(View.INVISIBLE);
                        }
                    });
                    anim.start();
                }else{
                    // get the center for the clipping circle
                    int cx = (txt_reveal.getLeft() + txt_reveal.getRight()) / 2;
                    int cy = (txt_reveal.getTop() + txt_reveal.getBottom()) / 2;
                    // get the final radius for the clipping circle
                    int finalRadius = Math.max(txt_reveal.getWidth(), txt_reveal.getHeight());
                    // create the animator for this view (the start radius is zero)
                    Animator anim =
                            ViewAnimationUtils.createCircularReveal(txt_reveal, cx, cy, 0, finalRadius);
                    // make the view visible and start the animation
                    txt_reveal.setVisibility(View.VISIBLE);
                    anim.start();
                }
            }
        });

        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                imageView.animate()
                        .translationX(200)
                        .translationY(500)
                        .withEndAction(new Runnable() {
                            @Override
                            public void run() {
//                                int cx = (view.getLeft() + view.getRight()) / 2;
//                                int cy = (view.getTop() + view.getBottom()) / 2;
                                int cx = (int)imageView.getX();
                                int cy = (int)imageView.getY();
                                // get the final radius for the clipping circle
                                int finalRadius = Math.max(view.getWidth(), view.getHeight());
                                // create the animator for this view (the start radius is zero)
                                Animator anim =
                                        ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, finalRadius);
                                // make the view visible and start the animation
                                Palette palette = Palette.generate(BitmapFactory
                                        .decodeResource(getResources(),R.mipmap.ic_launcher));
                                view.setBackgroundColor(palette.getLightVibrantSwatch().getRgb());
                                view.setVisibility(View.VISIBLE);
                                imageView.setVisibility(View.INVISIBLE);
                                anim.start();
                            }
                        })
                        .start();
            }
        });
    }
}
